﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ActUtlTypeLib;

namespace Commonality.Model
{
    public class MitsubisiPlcModel
    {
        private static int address;
        private static int offset;
        public static List<ActUtlTypeClass> MitsubisiPLC;

        #region 变量
        #region MyRegion
        public static List<int[]> DxIn;
        public static List<int[]> DxOut;
        public static List<int[]> DxOutBuff;
        #endregion


        public static List<int[]> DxxxIn;
        public static List<int[]> MxxxIn;
        public static List<int[]> DxxxOut;
        public static List<int[]> MxxxOut;

        public static List<int[]> DxxxOutBuff;
        public static List<int[]> MxxxOutBuff;

        #endregion
        public static void MitsubisiPlcModelIni(int Address, int Offset)
        {
            MitsubisiPLC = new List<ActUtlTypeClass>(Offset);
            DxxxIn = new List<int[]>(offset);
            MxxxIn = new List<int[]>(offset);
            DxxxOut = new List<int[]>(offset);
            MxxxOut = new List<int[]>(offset);
            DxxxOutBuff = new List<int[]>(offset);
            MxxxOutBuff = new List<int[]>(offset);
            #region MyRegion
            DxIn = new List<int[]>(offset);
            DxOut = new List<int[]>(offset);
            DxOutBuff = new List<int[]>(offset);
            #endregion
            for (int i = 0; i < Offset; i++)
            {
                MitsubisiPLC.Add(new ActUtlTypeClass());
                DxxxIn.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                MxxxIn.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                DxxxOut.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                MxxxOut.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                DxxxOutBuff.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                MxxxOutBuff.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });

                #region MyRegion
                DxIn.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                DxOut.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                DxOutBuff.Add(new int[32] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
                #endregion
                MitsubisiPLC[i].ActLogicalStationNumber = i + Address;
            }
            address = Address;
            offset = Offset;
        }
        #region 读取
        //public static bool ReadBit(ushort stationNumber, string device)
        //{
        //    int data = 0x00;
        //    int str = MitsubisiPLC[stationNumber - 1].ReadDeviceRandom(device, 1, out data);
        //    return data > 0 ? true : false;
        //}
        public static byte ReadBit(ushort stationNumber, string device)
        {
            int data = 0x00;
            int str = MitsubisiPLC[stationNumber - address].ReadDeviceRandom(device, 1, out data);
            return (byte)data;
        }
        public static Int16 ReadBytes(ushort stationNumber, string device)
        {
            int data = 0x00;
            int str = MitsubisiPLC[stationNumber - address].ReadDeviceRandom(device, 1, out data);
            return Convert.ToInt16(data);
        }
        public static Int32 ReadDoubleBytes(ushort stationNumber, string device)
        {
            int data = 0x0000;
            int str = MitsubisiPLC[stationNumber - address].ReadDeviceRandom(device, 2, out data);
            return Convert.ToInt16(data);
        }
        #endregion
        #region 写入
        public static void WriteBit(ushort stationNumber, string device, int data)
        {
            int str = MitsubisiPLC[stationNumber - address].WriteDeviceRandom(device, 1, ref data);
        }
        public static void WriteBytes(ushort stationNumber, string device, int data)
        {
            int str = MitsubisiPLC[stationNumber - address].WriteDeviceRandom(device, 1, ref data);
        }
        public static void WriteDoubleBytes(ushort stationNumber, string device, int data)
        {
            int str = MitsubisiPLC[stationNumber - address].WriteDeviceRandom(device, 2, ref data);
        }
        #endregion

        #region 批量读取
        public static int[] ReadBytesBlock(ushort stationNumber, string device)
        {
            int[] data = new int[32];
            int str = MitsubisiPLC[stationNumber - address].ReadDeviceBlock(device, data.Length, out data[0]);
            return data;
        }
        #endregion

        #region 批量写入
        public static void WriteBytesBlock(ushort stationNumber, string device,ref int[] data)
        {
            int str = MitsubisiPLC[stationNumber - address].WriteDeviceBlock(device, 32, ref data[0]);
        }
        #endregion
        public static string Open(ushort StationNumber)
        {
            string ret = "";
            switch (MitsubisiPLC[StationNumber - address].Open())
            {
                case 0:
                    ret = "连接完成";
                    break;
                case 1:
                    ret = "1";
                    break;
                case 2:
                    ret = "2";
                    break;
                default:
                    ret = "连接失败";
                    break;
            }
            return ret;
        }
        public static string Close(ushort StationNumber)
        {
            string ret = "";
            switch (MitsubisiPLC[StationNumber - address].Close())
            {
                case 0:
                    ret = "";
                    break;
                case 1:
                    ret = "";
                    break;
                case 2:
                    ret = "";
                    break;
                default:
                    break;
            }
            return ret;
        }
    }

}
